/*
 * @Description: 多国语言入口
 * @Author: xuzp4
 */
import Vue from 'vue'
import VueI18n from 'vue-i18n'
import Cookies from 'js-cookie'
import elementEnLocale from 'element-ui/lib/locale/lang/en' // element-ui lang
import elementZhLocale from 'element-ui/lib/locale/lang/zh-CN' // element-ui lang
import elementZhTWLocale from 'element-ui/lib/locale/lang/zh-TW' // element-ui lang

import enLocale from './en'
import zhLocale from './zh'

import zhLocaleError from './message_zh'
import enLocaleError from './message_en'

import zhtwLocale from './zhtw'
import zhtwLocaleError from './message_zhtw'

Vue.use(VueI18n)

const messages = {
  'en-US': {
    ...enLocale,
    ...elementEnLocale,
    ...enLocaleError,
  },
  'zh-CN': {
    ...zhLocale,
    ...elementZhLocale,
    ...zhLocaleError,
  },
  'zh-TW':{
    ...zhtwLocale,
    ...zhtwLocaleError,
    ...elementZhTWLocale,
  }
}
export function getLanguage() {
  const chooseLanguage = Cookies.get('language')
  if (chooseLanguage) return chooseLanguage
  // if has not choose language
  const language = navigator.language || navigator.browserLanguage
  const locales = Object.keys(messages)
  for (var locale of locales) {
    if (language.indexOf(locale) > -1) {
      return language
    }
  }
  return 'zh-TW'
}
const i18n = new VueI18n({
  // set locale
  // options: en | zh | es
  locale: getLanguage(),
  fallbackLocale:'zh-TW',
  // set locale messages
  messages,
  silentTranslationWarn: true,
})
export default i18n
